﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using com.tiyma.common;

namespace com.tiyma.components
{
    public class ProxyManager
    {
        private static ProxyManager m_instance = null;
        private static object m_obj = new object();

        Queue<WebProxy> m_proxy_queue = null;

        public static ProxyManager Instance
        {
            get
            {

                lock (m_obj)
                {
                    if (m_instance == null)
                    {
                        ProxyManager t_proxy_mgr = new ProxyManager();
                        t_proxy_mgr.LoadPorxyList();
                        m_instance = t_proxy_mgr;

                    }
                }

                return m_instance;
            }
        }

        /// <summary>
        /// 获取代理
        /// </summary>
        /// <returns></returns>
        public WebProxy GetProxy()
        {
            if (m_proxy_queue == null || m_proxy_queue.Count == 0)
                LoadPorxyList();

            if (m_proxy_queue == null || m_proxy_queue.Count == 0)
                return null;

            WebProxy t_web_proxy = m_proxy_queue.Dequeue();

            return t_web_proxy;

        }

        /// <summary>
        /// 设置代理
        /// </summary>
        /// <param name="i_web_proxy"></param>
        public void SetProxy(WebProxy i_web_proxy)
        {
            if (m_proxy_queue == null || m_proxy_queue.Count == 0)
                LoadPorxyList();

            if (m_proxy_queue == null || m_proxy_queue.Count == 0)
                return;

            m_proxy_queue.Enqueue(i_web_proxy);
        }

        /// <summary>
        /// 加载配置
        /// </summary>
        private void LoadPorxyList()
        {
            string t_file_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "configure");
            string t_proxy_file_name = Path.Combine(t_file_path, "proxy.txt");
            if (!File.Exists(t_proxy_file_name))
                return;

            m_proxy_queue = new Queue<WebProxy>();

            try
            {
                using (StreamReader t_sr_reader = new StreamReader(t_proxy_file_name))
                {
                    string t_line = string.Empty;
                    string[] t_arr = null;
                    int t_port = 0;
                    while ((t_line = t_sr_reader.ReadLine()) != null)
                    {
                        if (string.IsNullOrEmpty(t_line))
                            continue;

                        t_arr = t_line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (t_arr == null || t_arr.Length != 2)
                            continue;

                        if (!int.TryParse(t_arr[1], out t_port))
                            continue;

                        WebProxy t_proxy_info = new WebProxy(t_arr[0], t_port);

                        m_proxy_queue.Enqueue(t_proxy_info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("加载代理配置失败" + ex.ToString());
            }
        }
    }
}
